<template>
  <div class="electric-monitor">
    <div>
      <label>{{ $t("route.Power_energy_consumption_management") }}</label>
      <div></div>
      <button>
        <img /><label>{{ $t("route.Energy_consuming_Supervising") }}</label>
      </button>
    </div>

    <div>
      <div>
        <div v-if="chartShow" id="echart-1" style="height: 250px; width: 100%"></div>
      </div>
      <div>
        <div>
          <div>{{ $t("route.Energy_consumption_of_the_day") }}</div>
          <div>
            <label>{{ todayElectric.toFixed(2) }}</label>
            <label>{{ $t("route.degree") }}</label>
          </div>
        </div>
        <div>
          <div v-if="chartShow" id="echart-3" style="height: 250px; width: 100%"></div>
        </div>
      </div>
    </div>

    <div>
      <div>
        <el-cascader
          size="default"
          style="width: 150px"
          :options="options"
          @change="proLineChange"
        />
        <el-date-picker
          size="default"
          style="margin-left: 10px; width: 150px"
          @change="createTimeChange"
          v-model="query.createTime"
          type="date"
          :placeholder="$t('route.Select_date')"
        />
        <div style="margin-left: 10px">
          <el-button style="color: #99cc00" @click="bootUpClick">{{
            $t("route.One_button_startup")
          }}</el-button>
          <el-button style="color: #ff0000" @click="shutDownClick">{{
            $t("route.SimJet_Soft")
          }}</el-button>
          <el-button style="color: #ff0000" @click="interruptClick">{{
            $t("route.One_button_setting_power_off")
          }}</el-button>
        </div>
        <div class="div-handle-right">
          <el-button class="mgr-left-10" @click="searchClick">{{
            $t("route.search")
          }}</el-button>
          <input
            class="mgr-left-10"
            v-model="query.search"
            :placeholder="$t('route.Head_Model')"
          />
          <!-- <el-button class="mgr-left-10" @click='interruptClick()'>修改断电时长</el-button> -->
          <!-- <el-input-number class="mgr-left-10" v-model="interrupt" :min="0" :step="1" /> -->
          <el-button class="mgr-left-10" style="color: #99cc00" @click="selBootUpClick">{{
            $t("route.Startup")
          }}</el-button>
          <el-button style="color: #ff0000" @click="selShutDownClick">{{
            $t("route.Shutdown")
          }}</el-button>
          <el-button style="color: #ff0000" @click="selInterruptClick">{{
            $t("route.Set_power_off")
          }}</el-button>
        </div>
      </div>
      <div>
        <el-table
          @selection-change="selectionLineChangeHandle"
          :data="deviceElectricList"
          style="width: 100%"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="kind" :label="$t('route.kind')" />
          <el-table-column prop="noseCode" :label="$t('route.Head_Model')" />
          <el-table-column :label="$t('route.Startup_duration')">
            <template #default="scope">
              {{ formatDur(scope.row.bootUpDur) }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('route.Sewing_time')">
            <template #default="scope">
              {{ formatDur(scope.row.workDur) }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('route.Power_consumption')">
            <template #default="scope">
              {{ scope.row.electric ? scope.row.electric.toFixed(2) : 0 }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('route.Power_consumption_cost')">
            <template #default="scope">
              {{ scope.row.electric ? (scope.row.electric * 1.025).toFixed(2) : 0 }}
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination">
        <el-pagination
          style="float: right"
          background
          @current-change="handlePageChange"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          layout="total, prev, pager, next"
          :total="query.total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import * as echarts from "echarts";
import CRC from "../../js/common/crc.js";
import ParamUtils from "../../js/common/param-utils.js";
import { ElMessageBox } from "element-plus";
import { markRaw } from "vue";
import i18n from "../../i18n/index";
import Constant from "../../js/common/constant.js";
export default {
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        workshop: null,
        proLine: null,
        search: null,
        createTime: new Date(),
      },
      deviceElectricList: [],
      chartShow: false,
      todayElectric: 0,
      options: [],
      checkRows: [],
    };
  },
  methods: {
    selectionLineChangeHandle(val) {
      this.checkRows = val;
      console.log(JSON.stringify(this.checkRows));
    },
    proLineChange(value) {
      this.query.workshop = value[0];
      this.query.proLine = value[1];
      this.query.pageNum = 1;
      this.getDeviceElectricList();
    },
    interruptClick() {
      var content = i18n.global.t("route.modify");
      if (this.query.workshop == null) {
        content += i18n.global.t("route.All_workshops");
      } else {
        content += this.query.workshop + "/";
      }
      if (this.query.proLine == null) {
        content += i18n.global.t("route.All_groups");
      } else {
        content += this.query.proLine;
      }
      content += i18n.global.t("route.device1");
      ElMessageBox.prompt(content, i18n.global.t("route.Modify_power_off_duration"), {
        confirmButtonText: i18n.global.t("route.sure"),
        cancelButtonText: i18n.global.t("route.cancel"),
        inputType: "number",
      }).then(({ value }) => {
        var cmd = ParamUtils.format11Cmd(0, 0x03, value);
        this.sendBatchCmd(cmd, () => {
          this.$message.success(i18n.global.t("route.continue4"));
        });
      });
    },
    selInterruptClick() {
      var deviceIdList = [];
      for (var item of this.checkRows) {
        deviceIdList.push(item.id);
      }
      if (deviceIdList.length == 0) {
        this.$message.error(i18n.global.t("route.Please_select_a_device"));
        return;
      }
      ElMessageBox.prompt(
        i18n.global.t("route.continue5"),
        i18n.global.t("route.Modify_power_off_duration"),
        {
          confirmButtonText: i18n.global.t("route.sure"),
          cancelButtonText: i18n.global.t("route.cancel"),
          inputType: "number",
        }
      ).then(({ value }) => {
        var cmd = ParamUtils.format11Cmd(0, 0x03, value);
        this.sendListCmd(deviceIdList, cmd, () => {
          this.$message.success(i18n.global.t("route.continue4"));
        });
      });
    },
    shutDownClick() {
      var content = i18n.global.t("route.This_action_will_close");
      if (this.query.workshop == null) {
        content += i18n.global.t("route.All_workshops");
      } else {
        content += this.query.workshop + "/";
      }
      if (this.query.proLine == null) {
        content += i18n.global.t("route.All_groups");
      } else {
        content += this.query.proLine;
      }
      content += i18n.global.t("route.Do_you_want_to_continue");
      this.$confirm(content, i18n.global.t("route.reminder"), {
        confirmButtonText: i18n.global.t("route.sure"),
        cancelButtonText: i18n.global.t("route.cancel"),
        type: "warning",
      }).then(() => {
        var cmd = ParamUtils.format14Cmd(0, 0x02);
        this.sendBatchCmd(cmd, () => {
          this.$message.success(i18n.global.t("route.The_device_is_shutting_down"));
        });
      });
    },
    selShutDownClick() {
      var deviceIdList = [];
      for (var item of this.checkRows) {
        deviceIdList.push(item.id);
      }
      if (deviceIdList.length == 0) {
        this.$message.error(i18n.global.t("route.Please_select_a_device"));
        return;
      }
      this.$confirm(i18n.global.t("route.continue6"), i18n.global.t("route.reminder"), {
        confirmButtonText: i18n.global.t("route.sure"),
        cancelButtonText: i18n.global.t("route.cancel"),
        type: "warning",
      }).then(() => {
        var cmd = ParamUtils.format14Cmd(0, 0x02);
        this.sendListCmd(deviceIdList, cmd, () => {
          this.$message.success(i18n.global.t("route.The_device_is_shutting_down"));
        });
      });
    },
    bootUpClick() {
      var content = i18n.global.t("route.This_operation_will_turn_on");
      if (this.query.workshop == null) {
        content += i18n.global.t("route.All_workshops");
      } else {
        content += this.query.workshop + "/";
      }
      if (this.query.proLine == null) {
        content += i18n.global.t("route.All_groups");
      } else {
        content += this.query.proLine;
      }
      content += i18n.global.t("route.Do_you_want_to_continue");
      this.$confirm(content, i18n.global.t("route.reminder"), {
        confirmButtonText: i18n.global.t("route.sure"),
        cancelButtonText: i18n.global.t("route.cancel"),
        type: "warning",
      }).then(() => {
        var cmd = ParamUtils.format14Cmd(0, 0x03);
        this.sendBatchCmd(cmd, () => {
          this.$message.success(i18n.global.t("route.The_device_is_turning_on"));
        });
      });
    },
    selBootUpClick() {
      var deviceIdList = [];
      for (var item of this.checkRows) {
        deviceIdList.push(item.id);
      }
      if (deviceIdList.length == 0) {
        this.$message.error(i18n.global.t("route.Please_select_a_device"));
        return;
      }
      this.$confirm(i18n.global.t("route.continue7"), i18n.global.t("route.reminder"), {
        confirmButtonText: i18n.global.t("route.sure"),
        cancelButtonText: i18n.global.t("route.cancel"),
        type: "warning",
      }).then(() => {
        var cmd = ParamUtils.format14Cmd(0, 0x03);
        this.sendListCmd(deviceIdList, cmd, () => {
          this.$message.success(i18n.global.t("route.The_device_is_turning_on"));
        });
      });
    },
    sendBatchCmd(cmd, complete) {
      http.post(
        api.SEND_BATCH_CMD,
        {
          adminId: this.$store.state.user.id,
          workshop:
            this.query.workshop == i18n.global.t("route.All_workshops")
              ? null
              : this.query.workshop,
          proLine:
            this.query.proLine == i18n.global.t("route.All_groups")
              ? null
              : this.query.proLine,
          cmd: cmd,
        },
        (data) => {
          if (complete) complete();
        }
      );
    },
    sendListCmd(deviceIdList, cmd, complete) {
      http.post(
        api.SEND_LIST_CMD,
        {
          deviceIdList: deviceIdList,
          cmd: cmd,
        },
        (data) => {
          if (complete) complete();
        }
      );
    },
    getDeviceElectricList() {
      http.post(
        api.GET_DEVICE_ELECTRIC_LIST,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
          pageNum: this.query.pageNum,
          pageSize: this.query.pageSize,
          workshop:
            this.query.workshop == i18n.global.t("route.All_workshops")
              ? null
              : this.query.workshop,
          proLine:
            this.query.proLine == i18n.global.t("route.All_groups")
              ? null
              : this.query.proLine,
          search: this.query.search,
          createTime: this.query.createTime.format("yyyy-MM-dd"),
        },
        (data) => {
          this.deviceElectricList = data.data.deviceElectricList;
          this.query.total = data.data.total;
        }
      );
    },
    getTodayElectric() {
      http.post(
        api.GET_TODAY_ELECTRIC,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
        },
        (data) => {
          if (data.data) {
            this.todayElectric = data.data;
          }
        }
      );
    },
    getMonthElectricList() {
      http.post(
        api.GET_MONTH_ELECTRIC_LIST,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
        },
        (data) => {
          var xData = [];
          var yData = [];
          var dataAry = [];
          for (var item of data.data) {
            xData.push(item.month + i18n.global.t("route.month"));
            yData.push(item.num ? item.num.toFixed(2) : 0);
          }
          this.initEchart1(xData, yData);
        }
      );
    },
    getWorkshopElectricList() {
      http.post(
        api.GET_WORKSHOP_ELECTRIC_LIST,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
          createTime: new Date().format("yyyy-MM-dd"),
        },
        (data) => {
          var dataAry = [];
          for (var item of data.data) {
            dataAry.push({
              name: item.workshop,
              value: item.num.toFixed(2),
            });
          }
          this.initEchart3(dataAry);
        }
      );
    },

    handlePageChange(pageNum) {
      this.query.pageNum = pageNum;
      this.getDeviceElectricList();
    },
    createTimeChange(pageNum) {
      this.query.pageNum = 1;
      this.getDeviceElectricList();
    },
    formatDur(dur) {
      if (dur == null || dur == undefined) return "----";
      return CommonUtils.formatDur(dur);
    },

    searchClick() {
      this.query.pageNum = 1;
      this.getDeviceElectricList();
    },

    initEchart1(xData, yData) {
      var myChart = markRaw(echarts.init(document.getElementById("echart-1")));
      var option = CommonUtils.initBarOption(
        i18n.global.t("route.Power_consumption"),
        xData,
        yData
      );
      option.series[0].realtimeSort = false;
      myChart.setOption(option);
    },

    initEchart3(data) {
      var myChart = echarts.init(document.getElementById("echart-3"));
      var option = CommonUtils.initPieOption(
        i18n.global.t("route.Workshop_power_consumption_in_this_month"),
        i18n.global.t("route.Power_consumption"),
        data,
        null,
        i18n.global.t("route.degree")
      );
      myChart.setOption(option);
    },
    getPorLineRow() {
      http.post(
        api.GET_POR_LINE_ROW,
        {
          adminId: this.$store.state.user.id,
        },
        (data) => {
          for (var item of data.data) {
            var children = item.children;
            children.unshift({
              value: i18n.global.t("route.All_groups"),
              label: i18n.global.t("route.All_groups"),
            });
          }
          this.options = data.data;
          this.options.unshift({
            value: i18n.global.t("route.All_workshops"),
            label: i18n.global.t("route.All_workshops"),
          });
        }
      );
    },
  },
  created() {
    this.chartShow = true;
    this.getDeviceElectricList();
    this.getMonthElectricList();
    this.getWorkshopElectricList();
    this.getTodayElectric();
    this.getPorLineRow();
  },

  mounted() {},
};
</script>
<style lang="scss" scoped>
.electric-monitor {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  > div:not(:nth-child(2n)) {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;
  }

  .select {
    width: 120px;
    margin-left: 10px;
  }

  > div:nth-of-type(1) {
    > label:nth-of-type(1) {
      font-weight: 600;
    }

    > div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    > button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      > img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }

    > div:nth-of-type(2) {
      flex: 1;
      flex-direction: row-reverse;
      text-align: right;
      display: flex;
      align-items: center;

      > button:nth-last-of-type(1) {
        padding: 4px 20px;
        color: #ff0000;
        background-repeat: no-repeat;
      }

      > button:nth-last-of-type(2) {
        margin-left: 10px;
        padding: 4px 20px;
        color: #99cc00;
      }
    }
  }

  > div:nth-of-type(2) {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;

    > div {
      background-color: #ffffff;
      padding: 20px;
      border-radius: 5px;
      flex: 1;
    }

    > div:nth-of-type(1) {
      margin-right: 10px;
    }

    > div:nth-of-type(2) {
      margin-left: 10px;
      display: flex;
      flex-direction: row;
      align-items: center;

      > div:nth-of-type(1) {
        flex: 1;
        font-size: 30px;
        text-align: center;
        font-weight: 800;

        > div:nth-of-type(2) {
          font-size: 60px;

          > label:nth-of-type(2) {
            font-size: 30px;
            margin-left: 10px;
          }
        }
      }

      > div:nth-of-type(2) {
        flex: 1;
      }
    }
  }

  > div:nth-of-type(3) {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    margin-top: 20px;
    background-color: #ffffff;

    > div {
      width: 100%;
    }

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 150px;
          padding: 5px 8px;
          outline: none;
        }

        > button:nth {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 20px;
        }
      }
    }
  }
}
</style>
